"""
用户接口

"""
import os

from flask import Blueprint, request
from werkzeug.utils import secure_filename

from controller.user import *
from models.user import UserLog
from utils.permission import authorize
from utils.views import valid, resp_with_data
from components.base_exception import ParamsError
from components.base_log import log_method_call

user_bp = Blueprint('user', __name__)


@user_bp.route('/add_user', methods=['POST'])
@authorize("system:user:add")
@log_method_call(action='Create')
def add_user():
    """
    添加子用户
    """
    phone = g.json.get('phone')
    name = g.json.get('name')
    org_id = g.json.get('org_id')
    valid_res = valid(
        (phone, '手机号必填'),
        (name, '姓名必填'),
        (org_id, '组织机构id必填'),
    )
    if valid_res:
        raise ParamsError(data=valid_res)
    add_user_for_org()
    return resp_with_data(10000)


@user_bp.route('/users', methods=['GET'])
@authorize("system:*:*")
@log_method_call(action='Retrieve')
def users():
    """
    获取当前组织下的所有用户
    """
    data = get_user()
    return resp_with_data(10000, data=data)


@user_bp.route('/user_info', methods=['GET'])
@authorize("system:*:*")
@log_method_call(action='Retrieve')
def user_info():
    """
    获取用户详情
    包含menu、permission、user
    """
    data = get_user_info()
    return resp_with_data(10000, data=data)


@user_bp.route('/member_info/<pk>', methods=['GET'])
@authorize("system:*:*")
@log_method_call(action='Retrieve')
def member_info(pk):
    """
    获取用户详情
    包含menu、permission、user
    """
    data = get_member_info(pk)
    return resp_with_data(10000, data=data)


@user_bp.route('/update_user', methods=['POST'])
@authorize("system:user:update")
@log_method_call(action='Update')
def update_user():
    """
    修改当前登录的成员信息
    """
    update_user_for_org()
    return resp_with_data(10000)


@user_bp.route('/enable_user', methods=['POST'])
@authorize("system:user:update")
@log_method_call(action='Update')
def enable_user():
    """
    启用子账号
    """
    user_id = g.json.get('user_id')
    valid_res = valid(
        (user_id, '用户id必填'),
    )
    if valid_res:
        raise ParamsError(data=valid_res)
    user_enable_status(True)
    return resp_with_data(10000)


@user_bp.route('/disable_user', methods=['POST'])
@authorize("system:user:update")
@log_method_call(action='Update')
def disable_user():
    """
    禁用子账号
    """
    user_id = g.json.get('user_id')
    valid_res = valid(
        (user_id, '用户id必填'),
    )
    if valid_res:
        raise ParamsError(data=valid_res)
    user_enable_status(False)
    return resp_with_data(10000, data=True)


@user_bp.route('/delete_user', methods=['POST'])
@authorize("system:user:update")
@log_method_call(action='Update')
def delete_user():
    """
    启用子账号
    """
    user_id = g.json.get('user_id')
    valid_res = valid(
        (user_id, '用户id必填'),
    )
    if valid_res:
        raise ParamsError(data=valid_res)
    user_delete_status()
    return resp_with_data(10000, data=True)


@user_bp.route('/update_user_logo', methods=['POST'])
@authorize("system:*:*")
@log_method_call(action='Update')
def update_user_logo():
    file = request.files['file']
    if file:
        # 文件的保存路径
        filepath = os.path.join('/upload/', secure_filename(file.filename))
        # 保存文件到服务器
        file.save(filepath)
        update_logo_for_user(filepath)
        return resp_with_data(10000)


@user_bp.route('/recorded', methods=['POST'])
@authorize("system:*:*")
@log_method_call(action='Create')
def recorded():
    type = g.json.get('type')
    source = g.json.get('source')
    params = g.json.get('params')
    valid_res = valid(
        (type, '埋点类型必填'),
        (source, '来源必填')
    )
    if valid_res:
        raise ParamsError(code=1405, data=valid_res)
    UserLog.create(
        type=type,
        user_id=g.user_id,
        source=source,
        params=params if params else ''
    )
    return resp_with_data(10000)


# 待删除
@user_bp.route('/auths', methods=['POST'])
@authorize("system:user:auths")
@log_method_call(action='Retrieve')
def get_auths():
    """
    获取权限列表
    """
    user_id = g.json.get('user_id')
    valid_res = valid(
        (user_id, '用户id必填'),
    )
    if valid_res:
        raise ParamsError(data=valid_res)
    data = get_user_auth()
    return resp_with_data(10000, data=data)


@user_bp.route('/update_auth', methods=['POST'])
@authorize("system:user:update_auth")
@log_method_call(action='Update')
def update_auths():
    """
    修改权限
    """
    user_id = g.json.get('user_id')
    code_list = g.json.get('code_list')
    valid_res = valid(
        (code_list, '权限code表必填'),
        (user_id, '用户id必填'),
    )
    if valid_res:
        raise ParamsError(data=valid_res)
    update_user_auth()
    return resp_with_data(10000)
# --------------------------
